<template>
	<view>
		<view class="list" v-for="(item, index) in list">
			<view>
				<view>医生姓名：</view>
				<view>{{ item.name }}</view>
				<view>
					<text @click="targetMyReserveDetails(item.id)">查看更多</text>
				</view>
			</view>
			<view>
				<view>科室：</view>
				<view>{{ item.title }}</view>
			</view>
			<view class="status">
				<view>状态：</view>
				<view><text :data-status="item.status">{{ getStatus(item.status) }}</text></view>
				<view v-if="item.status === 2">
					<view><text @click="cancelReserve(item.id, item.status === 2)">取消预约</text></view>
					<view><text @click="targetEditorReserve(item, item.status === 2)">修改预约</text></view>
				</view>
			</view>
			<view class="createTime">
				<view>
					<view>创建时间：{{ parseTime(item.createtime) }}</view>
					<view>预约时间：{{ item.reservatio_time }}</view>
				</view>
			</view>
		</view>

		<u-toast ref="uToast"></u-toast>

	</view>
</template>

<script>
import {
	record,
	cancelReservatio
} from '../../request/index.js'
export default {
	data() {
		return {
			list: [],
		}
	},
	onShow() {


	},
	onLoad() {
		this.request();
	},
	methods: {
		parseTime(timestamp) {
			var date = new Date(timestamp * 1000)
			var Y = date.getFullYear() + '-'
			var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
			const D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' '
			const h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':'
			const m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':'
			const s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds())
			return Y + M + D + h + m + s
		},
		targetEditorReserve(item, status) {
			if (status) {
				uni.setStorageSync('editReserve', item)
				uni.navigateTo({
					url: '/pages/editReserve/index',
				})
			}
		},
		request() {
			record({
				patientid: uni.getStorageSync('userid')
			}).then(res => {
				console.log(res);
				this.list = res.data.list.sort((a, b) => b.createtime - a.createtime);
			})
		},
		cancelReserve(id, status) {
			const that = this;
			console.log(id, status);
			if (id && status) {
				uni.showModal({
					title: '提示',
					content: '确认取消预约',
					success(res) {
						if (res.confirm) {
							cancelReservatio({
								id: id
							}).then(res => {
								if (res.data.status) {
									that.$refs.uToast.show({
										message: res.data.msg
									})
									that.request();
								}
							})
						}
					}
				})
			}
		},
		getStatus(status) {
			switch (status) {
				case 0:
					return '已取消';
				case 1:
					return '已处理';
				case 2:
					return '未处理';
				case 3:
					return '已住院';
				case 4:
					return '已出院';
			}
		},
		targetMyReserveDetails(id) {
			uni.navigateTo({
				url: '/pages/myReserveDetails/index?id=' + id,
			})
		},
	},
}
</script>

<style lang="scss">
.list {
	margin: 1.5vh 3.5%;
	background: white;
	padding: 1vh 2.5%;
	border-radius: 7px;
	box-shadow: 0 0 0 1px #e8e8e8;

	>view {
		display: grid;
		grid-template-columns: repeat(3, 1fr);
		margin: 1vh 0;
		font-size: 15px;
		align-items: center;

		&:first-child>view:last-child {
			text-align: right;
			color: #2570fe;
			font-size: 15.5px;
		}

		&.createTime {
			margin-top: 1vh;
			font-size: 13px;
			color: #4d4b4b;
			width: 100%;
			display: flex;
			justify-content: flex-end;

			>view {
				display: flex;
				flex-wrap: wrap;
				justify-content: flex-end;
			}
		}

		&.status {
			>view:nth-child(2)>text {
				color: white;
				padding: .5vh 2vw;
				vertical-align: middle;
				font-size: 15.5px;
				border-radius: 5px;

				&[data-status='0'] {
					background: #686868;
				}

				&[data-status='1'] {
					background: black;
				}

				&[data-status='2'] {
					background: #ff0000;
				}

				&[data-status='3'] {
					background: #03a4f6;
				}

				&[data-status='4'] {
					background: #28da70;
				}
			}

			>view:last-child {
				>view:first-child {
					margin-bottom: 2vh;

					>text {
						background: #ff0000;
						color: white;
						padding: .5vh 2vw;
						vertical-align: middle;
						font-size: 15.5px;
						border-radius: 5px;
					}
				}

				>view:last-child>text {
					color: white;
					background: #03a4f6;
					padding: .5vh 2vw;
					vertical-align: middle;
					font-size: 15.5px;
					border-radius: 5px;
				}
			}
		}
	}
}
</style>
